package com.robam.gulimall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.robam.common.utils.PageUtils;
import com.robam.common.utils.Query;

import com.robam.gulimall.product.dao.CategoryDao;
import com.robam.gulimall.product.entity.CategoryEntity;
import com.robam.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> getTree() {
        // 1.查询出所有分类
        List<CategoryEntity> entities = baseMapper.selectList(null);
        // 2.找出所有一级分类
        List<CategoryEntity> list = entities.stream()
            .filter(item -> item.getParentCid() == 0)
            .map((item) -> {
                item.setChildren(getChildren(item, entities));
                return item;
            })
//                .sorted((item1, item2) -> {
//                    return (item1.getSort() == null ? 0 : item1.getSort()) - (item2.getSort() == null ? 0 : item2.getSort());
//                    return item1.getSort().compareTo(item2.getSort());
//                })
            .sorted((item1, item2) -> (item1.getSort() == null ? 0 : item1.getSort()) - (item2.getSort() == null ? 0 : item2.getSort()))
//                .sorted((item1, item2) -> item1.getSort().compareTo(item2.getSort()))
            .collect(Collectors.toList());
        return list;
    }

    @Override
    public void removeCategoryByIds(List<Long> asList) {
        // TODO 检查分类id是否被引用

        baseMapper.deleteBatchIds(asList);
    }

    @Override
    public Long[] getCategoryPath(Long catelogId) {
        if(catelogId == null) {
            return new Long[0];
        }
        List<Long> categoryPath = new ArrayList<>();
        categoryPath.add(catelogId);
        while(true) {
            CategoryEntity categoryEntity = this.baseMapper.selectById(catelogId);
            if(categoryEntity.getParentCid() != 0) {
                catelogId = categoryEntity.getParentCid();
                categoryPath.add(catelogId);
            } else {
                break;
            }
        }
        Collections.reverse(categoryPath);
        return categoryPath.toArray(new Long[categoryPath.size()-1]);
    }

    private List<CategoryEntity> getChildren(CategoryEntity root, List<CategoryEntity> all) {
        // 从全部数据all里面找到当前节点root的子节点
        List<CategoryEntity> list = all.stream()
                .filter(item -> item.getParentCid().equals(root.getCatId()))
                .map((item) -> {
                    item.setChildren(getChildren(item, all));
                    return item;
                })
                .sorted((item1, item2) -> (item1.getSort() == null ? 0 : item1.getSort()) - (item2.getSort() == null ? 0 : item2.getSort()))
                .collect(Collectors.toList());
        return list;
    }

}